package util;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.ItemStateListener;

public class KeyListeningItem extends CustomItem {

	String lastCommand = "None";

	String displayText = "";

	ItemStateListener parent;

	public KeyListeningItem(String _title) {
		super(_title);
		// setLayout(LAYOUT_CENTER);
	}

	public void setItemStateListener(ItemStateListener _il) {
		parent = _il;
	}

	public void refresh() {

		repaint();
	}

	protected int getMinContentWidth() {
		// TODO Auto-generated method stub
		return 250;
	}

	protected int getMinContentHeight() {
		// TODO Auto-generated method stub
		return 200;
	}

	protected int getPrefContentWidth(int arg0) {
		// TODO Auto-generated method stub
		return 350;
	}

	protected int getPrefContentHeight(int arg0) {
		// TODO Auto-generated method stub
		return 300;
	}

	public void setString(String _s) {
		displayText = _s;
		repaint();
	}

	public void addString(String _s) {
		displayText = displayText + _s;
		repaint();
	}

	public String getLastCommand() {
		return lastCommand;
	}

	public void commandAction(Command c, Displayable d) {
		System.out.println("command");
	}
/*
	protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
                               int[] visRect_inout) {
		switch (dir) {
        case Canvas.RIGHT:
        	lastCommand = "Right";
        	System.out.println("rrrrrrrrrrright");
			parent.itemStateChanged(this);
           return true;
          //break;

        case Canvas.LEFT:
        	lastCommand = "Left";
        	System.out.println("lllllllllllllight");
			parent.itemStateChanged(this);
        return true;

         // break;
      }
      return false;

 
    }
*/
	protected void keyPressed(int keyCode) {
		if (keyCode == 52) {
			lastCommand = "Left";
			parent.itemStateChanged(this);
		} else if (keyCode == 54) {
			lastCommand = "Right";
			parent.itemStateChanged(this);
		} else{
			lastCommand = String.valueOf(keyCode);
			parent.itemStateChanged(this);
		}
	}

	protected void paint(Graphics g, int arg1, int arg2) {
		// TODO Auto-generated method stub
		String[] lines = Storage.split(displayText, (char) 10);
		for (int i = 0; i < lines.length; i++) {
			g.drawString(lines[i], 0, i * 22, 0);
		}
	}

}
